using Joy.Erp.BasicArchives.Core;
using Joy.Erp.BasicArchives.Core.Filters;
using Joy.Erp.BasicArchives.Permissions;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.Domain.Repositories;

namespace Joy.Erp.BasicArchives.Sales
{
    [Authorize]
    public class IntentOrderAppService : BasicArchivesAppService, IIntentOrderAppService
    {
        protected IIntentOrderRepository IntentOrderRepository => LazyServiceProvider.LazyGetRequiredService<IIntentOrderRepository>();
        protected IntentOrderManager IntentOrderManager => LazyServiceProvider.LazyGetRequiredService<IntentOrderManager>();

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<IntentOrderDto> GetAsync(Guid id)
        {
            var entity = await IntentOrderRepository.GetAsync(id, true);
            return ObjectMapper.Map<IntentOrder, IntentOrderDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<IntentOrderLineDto> GetChildAsync(Guid id)
        {
            var child = await IntentOrderRepository.GetChildAsync(id, true);
            return ObjectMapper.Map<IntentOrderLine, IntentOrderLineDto>(child);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<IntentOrderDto> FindAsync(string filter)
        {
            var entity = await IntentOrderRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<IntentOrder, IntentOrderDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<IntentOrderDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await IntentOrderRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<IntentOrder, IntentOrderDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<PagedResultDto<IntentOrderDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await IntentOrderRepository.GetCountAsync(input.Filter, true);
            var list = await IntentOrderRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<IntentOrderDto>(
                totalCount,
                ObjectMapper.Map<List<IntentOrder>, List<IntentOrderDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<PagedResultDto<IntentOrderDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<IntentOrder, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<IntentOrder>(input.FilterGroup);
            }

            var totalCount = await IntentOrderRepository.GetCountAsync(expression, true);
            var list = await IntentOrderRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<IntentOrderDto>(
                totalCount,
                ObjectMapper.Map<List<IntentOrder>, List<IntentOrderDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Query)]
        public async Task<PagedResultDto<IntentOrderLineDto>> QueryChildrenAsync(FilterGroupRequestDto input)
        {
            Expression<Func<IntentOrderLine, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<IntentOrderLine>(input.FilterGroup);
            }

            var totalCount = await IntentOrderRepository.GetChildrenCountAsync(expression, true);
            var list = await IntentOrderRepository.GetChildrenPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<IntentOrderLineDto>(
                totalCount,
                ObjectMapper.Map<List<IntentOrderLine>, List<IntentOrderLineDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Create)]
        public async Task<IntentOrderDto> CreateAsync(IntentOrderCreateDto input)
        {
            var entity = ObjectMapper.Map<IntentOrderCreateDto, IntentOrder>(input);

            await IntentOrderManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Update)]
        public async Task<IntentOrderDto> UpdateAsync(Guid id, IntentOrderUpdateDto input)
        {
            var entity = await IntentOrderRepository.GetAsync(id, true);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            await IntentOrderRepository.EnsureCollectionLoadedAsync(entity, x => x.Children);
            ObjectMapper.Map(input, entity);

            await IntentOrderManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Submit)]
        public async Task<IntentOrderDto> SubmitAsync(Guid id)
        {
            await IntentOrderManager.ChangeStatus(id, DocumentStatus.Waiting);
            return await GetAsync(id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Approve)]
        public async Task<IntentOrderDto> ApproveAsync(Guid id)
        {
            await IntentOrderManager.ChangeStatus(id, DocumentStatus.Approved);
            return await GetAsync(id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.UnApprove)]
        public async Task<IntentOrderDto> UnApproveAsync(Guid id)
        {
            await IntentOrderManager.ChangeStatus(id, DocumentStatus.Open);
            return await GetAsync(id);
        }

        [Authorize(BasicArchivesPermissions.IntentOrder.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await IntentOrderManager.DeleteAsync(id);
        }
    }
}